package com.ruoyi.delivery.controller;

import com.ruoyi.delivery.domain.DelCarrierInfo;
import com.ruoyi.delivery.service.IDelCarrierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物流商控制层
 *
 * @author hu
 */
@RestController
@RequestMapping("/delivery/carrier")
public class DelCarrierController {

    @Autowired
    private IDelCarrierService carrierService;

    /**
     * 获取物流商信息列表，支持分页和筛选
     *
     * @param carrierNo 物流商编号
     * @param carrierName 物流商名字
     * @param carrierType 物流商类型
     * @param carrierAddress 物流商地址
     * @param contactPhone 物流商联系电话
     * @param contactPerson 物流商负责人
     * @param contactEmail 负责人邮箱
     * @param activeStatus 物流商状态
     * @param page        页码
     * @param pageSize    每页记录数
     * @return 分页物流商信息列表
     */
    @GetMapping("/list")
    public Map<String, Object> getCarrierInfoList(
            @RequestParam(required = false) String carrierNo,
            @RequestParam(required = false) String carrierName,
            @RequestParam(required = false) Integer carrierType,
            @RequestParam(required = false) String carrierAddress,
            @RequestParam(required = false) String contactPhone,
            @RequestParam(required = false) String contactPerson,
            @RequestParam(required = false) Integer activeStatus,
            @RequestParam(required = false) String contactEmail,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        int offset = (page - 1) * pageSize;
        List<DelCarrierInfo> carrierInfos = carrierService.getCarrierInfoList(carrierNo, carrierName, carrierType, carrierAddress,
                contactPhone, contactPerson, contactEmail, activeStatus, offset, pageSize);
        int total = carrierService.getCarrierInfoCount(carrierNo, carrierName, carrierType, carrierAddress,
                contactPhone, contactPerson, contactEmail, activeStatus);

        Map<String, Object> response = new HashMap<>();
        response.put("data", carrierInfos);
        response.put("total", total);
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    /**
     * 新建物流商信息
     *
     * @param carrierInfo 物流商信息对象
     * @return 操作成功消息
     */
    @PostMapping("/add")
    public Map<String, String> insertCarrierInfo(@RequestBody DelCarrierInfo carrierInfo) {
        // 打印传入的数据内容
        System.out.println("Received DelCarrierInfo: " + carrierInfo);

        carrierService.insertCarrierInfo(carrierInfo);
        Map<String, String> response = new HashMap<>();
        response.put("message", "新建物流商信息成功");
        return response;
    }

    /**
     * 更新物流商信息
     *
     * @param carrierInfo 物流商信息对象
     * @return 操作成功消息
     */
    @PutMapping("/update")
    public Map<String, String> updateCarrierInfo(@RequestBody DelCarrierInfo carrierInfo) {
        carrierService.updateCarrierInfo(carrierInfo);
        Map<String, String> response = new HashMap<>();
        response.put("message", "更新物流商信息成功");
        return response;
    }

    /**
     * 删除物流商信息
     *
     * @param carrierNo 物流商编号
     * @return 操作成功消息
     */
    @DeleteMapping("/delete/{carrierNo}")
    public Map<String, String> deleteCarrierInfo(@PathVariable String carrierNo) {
        carrierService.deleteCarrierInfo(carrierNo);
        Map<String, String> response = new HashMap<>();
        response.put("message", "删除物流商信息成功");
        return response;
    }

    /**
     * 批量删除物流商信息
     *
     * @param carrierNos 物流商编号列表
     * @return 操作成功消息
     */
    @PostMapping("/batchDelete")
    public Map<String, String> deleteCarriers(@RequestBody List<String> carrierNos) {
        carrierService.deleteCarriers(carrierNos);
        Map<String, String> response = new HashMap<>();
        response.put("message", "批量删除物流商信息成功");
        return response;
    }
}
